package algebra.query.pd;

import java.util.HashSet;
import java.util.Set;

import option.QueryKeywords;
import utils.Display;

import entities.role.Role;

public class PdRole extends ProjDesc {

	private final Role r;
	private final ProjDesc sub;
	
	public PdRole(Role r, ProjDesc pd){
		this.r = r;
		this.sub = pd;
	}
	
	public ProjDesc getSubPd(){
		return sub;
	}

	@Override
	public boolean projectOnSomeFeature() {
		return false;
	}

	@Override
	public Set<Role> extractFeatures() {
		Set<Role> fs = new HashSet<Role>();
		fs.addAll(sub.extractFeatures());
		return fs;
	}

	public Role getRole() {
		return r;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((r == null) ? 0 : r.hashCode());
		result = prime * result + ((sub == null) ? 0 : sub.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof PdRole))
			return false;
		PdRole other = (PdRole) obj;
		if (r == null) {
			if (other.r != null)
				return false;
		} else if (!r.equals(other.r))
			return false;
		if (sub == null) {
			if (other.sub != null)
				return false;
		} else if (!sub.equals(other.sub))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "(" + Display.SOMEUNICODE + this.r.getRoleName() + "." 
				+ this.sub.toString() + ")";
	}

	@Override
	public String toSerializedString() {
		return "(" + QueryKeywords.PD_ROLE + " " + this.r.getRoleName() + " "
				+ this.sub.toSerializedString() + ")";
	}

	@Override
	public boolean moreSpecificThan(ProjDesc that) {
		if(that instanceof PdRole){
			return this.r.equals(((PdRole) that).r)
					&& this.sub.moreSpecificThan(((PdRole) that).sub);
		}
		return false;
	}

	@Override
	public boolean hasConjunctiveFeature() {
		return false;
	}
	
	
}
